package www.way2inbox.comm;

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.way2sms.way2inbox.Startup;

public class SessionManager {  
    SharedPreferences pref;     
    Editor editor;
    Context _context;
    int PRIVATE_MODE = 0;
    private static final String PREF_NAME = "way2smsapp";
    private static final String IS_LOGIN = "IsLoggedIn"; 
    public static final String KEY_NAME = "Username"; 
    public static final String KEY_TOKEN = "Token";  
    public static final String KEY_Mobile = "Mobile"; 
    
    // Constructor
    public SessionManager(Context context){
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }
     
    
//    Create login session  
    public void createLoginSession(String token,String name,String mobile){   	
        editor.putBoolean(IS_LOGIN, true);               
        editor.putString(KEY_NAME, name);
        System.out.println("dfdssssssssssssss............MAnager      "+token);
        editor.putString(KEY_TOKEN, token);
        System.out.println("dfdssssssssssssss      "+mobile);
        editor.putString(KEY_Mobile, mobile);     
        editor.commit();
    }  
    
    
//     Check login method will check user login status
    public void checkLogin(){
      
        if(!this.isLoggedIn()){
            // user is not logged in redirect him to Login Activity
            Intent i = new Intent(_context, Startup.class);
            // Closing all the Activities
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);            
            // Add new Flag to start new Activity
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            
            // Staring Login Activity
            _context.startActivity(i);
        }
         
    }
       
    
    // Get stored session data
    public HashMap<String, String> getUserDetails(){
        HashMap<String, String> user = new HashMap<String, String>();      
        user.put(KEY_NAME, pref.getString(KEY_NAME, null));             
        user.put(KEY_TOKEN, pref.getString(KEY_TOKEN, null)); 
        user.put(KEY_Mobile, pref.getString(KEY_Mobile, null));       
        return user;
    } 
     
      // Clear session details
    public void logoutUser(){
        // Clearing all data from Shared Preferences
        editor.clear();
        editor.commit();             
        Intent i = new Intent(_context, Startup.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);             
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        
        _context.startActivity(i);
    }
     // Get Login State
    public boolean isLoggedIn(){
        return pref.getBoolean(IS_LOGIN, false);
    }
}
